package cn.iocoder.yudao.module.call.controller.admin.tasknumber;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.call.controller.admin.tasknumber.vo.TaskNumberBatchSaveReqVO;
import cn.iocoder.yudao.module.call.controller.admin.tasknumber.vo.TaskNumberPageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.tasknumber.vo.TaskNumberRespVO;
import cn.iocoder.yudao.module.call.controller.admin.tasknumber.vo.TaskNumberSaveReqVO;
import cn.iocoder.yudao.module.call.dal.dataobject.tasknumber.TaskNumberDO;
import cn.iocoder.yudao.module.call.service.tasknumber.TaskNumberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 外呼名单")
@RestController
@RequestMapping("/call/task-number")
@Validated
public class TaskNumberController {

    @Resource
    private TaskNumberService taskNumberService;

    @PostMapping("/create")
    @Operation(summary = "外呼任务-添加号码")
    @PreAuthorize("@ss.hasPermission('call:task:create')")
    public CommonResult<Long> createTaskNumber(@Valid @RequestBody TaskNumberSaveReqVO createReqVO) {
        return success(taskNumberService.createTaskNumber(createReqVO));
    }

    @PostMapping("/batch-create")
    @Operation(summary = "外呼任务-批量添加号码")
    @PreAuthorize("@ss.hasPermission('call:task:create')")
    public CommonResult<Boolean> batchCreate(@Valid @RequestBody TaskNumberBatchSaveReqVO createReqVO) {
        taskNumberService.batchCreateTaskNumber(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新外呼名单")
    @PreAuthorize("@ss.hasPermission('call:task:update')")
    public CommonResult<Boolean> updateTaskNumber(@Valid @RequestBody TaskNumberSaveReqVO updateReqVO) {
        taskNumberService.updateTaskNumber(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除外呼名单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:task:delete')")
    public CommonResult<Boolean> deleteTaskNumber(@RequestParam("id") Long id) {
        taskNumberService.deleteTaskNumber(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得外呼名单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:task:query')")
    public CommonResult<TaskNumberRespVO> getTaskNumber(@RequestParam("id") Long id) {
        TaskNumberDO taskNumber = taskNumberService.getTaskNumber(id);
        return success(BeanUtils.toBean(taskNumber, TaskNumberRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得外呼名单分页")
    @PreAuthorize("@ss.hasPermission('call:task:query')")
    public CommonResult<PageResult<TaskNumberRespVO>> getTaskNumberPage(@Valid TaskNumberPageReqVO pageReqVO) {
        PageResult<TaskNumberDO> pageResult = taskNumberService.getTaskNumberPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskNumberRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出外呼名单 Excel")
    @PreAuthorize("@ss.hasPermission('call:task:export')")
    @OperateLog(type = EXPORT)
    public void exportTaskNumberExcel(@Valid TaskNumberPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaskNumberDO> list = taskNumberService.getTaskNumberPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "外呼名单.xls", "数据", TaskNumberRespVO.class,
                        BeanUtils.toBean(list, TaskNumberRespVO.class));
    }

}